package com.example.aidrawavatar.utils;

import com.example.aidrawavatar.service_interface.ImageService;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RetrofitUtils {
    private String url = "http://10.0.2.2:8999/";
    private static RetrofitUtils instance = null ;
    private ImageService imageService;
    private Retrofit retrofit = null ;
    private String imageStation = "http://10.0.2.2:80/GenerationAvatar/";

    private RetrofitUtils(){}

    public static RetrofitUtils getInstance() {
        if( instance == null ){
            synchronized (RetrofitUtils.class){
                if( instance == null ){
                    instance = new RetrofitUtils();
                    instance.initService();
                }
            }
        }
        return instance;
    }

    private void initService(){
        if( retrofit == null ){
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(30000, TimeUnit.MILLISECONDS)
                    .readTimeout(30000, TimeUnit.MILLISECONDS)
                    .writeTimeout(30000, TimeUnit.MILLISECONDS)
                    .build();

            retrofit = new Retrofit.Builder()
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(okHttpClient)
                    .baseUrl(url)
                    .build();

            imageService = retrofit.create(ImageService.class);
        }
    }

    public ImageService getImageService() {
        return imageService;
    }

    public String getImageStation() {
        return imageStation;
    }
}
